package com.example.samsungproject;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import com.example.samsungproject.*;
import com.example.entities.ContactEntity;
 
public class ContactActivity extends TabActivity {
	  Cursor model=null;
	  RestaurantAdapter adapter=null;
	  EditText name=null;
	  EditText number=null;
	  EditText notes=null;
	  Button add = null;
	  Button cancel = null;
	  RadioGroup types=null;
	  ContactEntity entity=null;
	  
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.contact_layout);
	    
	    entity=new ContactEntity(this);
	    
	    name=(EditText)findViewById(R.id.name);
	    number=(EditText)findViewById(R.id.number);
	    notes=(EditText)findViewById(R.id.notes);
	    types=(RadioGroup)findViewById(R.id.types);
	    
	    add=(Button)findViewById(R.id.add);
	    
	    add.setOnClickListener(onAdd);
	    
	    ListView list=(ListView)findViewById(R.id.contacts);
	    
	    model=entity.getAll();
	    startManagingCursor(model);
	    adapter=new RestaurantAdapter(model);
	    list.setAdapter(adapter);
	    
	    TabHost.TabSpec spec=getTabHost().newTabSpec("tag1");
	    
	    spec.setContent(R.id.contacts);
	    spec.setIndicator("List", getResources()
	                                .getDrawable(R.drawable.list));
	    getTabHost().addTab(spec);
	    
	    spec=getTabHost().newTabSpec("tag2");
	    spec.setContent(R.id.details);
	    spec.setIndicator("Details", getResources()
	                                  .getDrawable(R.drawable.restaurant));
	    getTabHost().addTab(spec);
	    
	    getTabHost().setCurrentTab(0);
	    
	    list.setOnItemClickListener(onListClick);
	  }
	  
	  @Override
	  public void onDestroy() {
	    super.onDestroy();
	    
	    entity.close();
	  }
	  
	  private View.OnClickListener onAdd=new View.OnClickListener() {
	    public void onClick(View v) {
	      String type=null;
	      
	      switch (types.getCheckedRadioButtonId()) {
	        case R.id.important:
	          type="important";
	          break;
	        case R.id.friend:
	          type="friend";
	          break;
	        case R.id.know:
	          type="know";
	          break;
	      }
	      
	      entity.insert(name.getText().toString(),
	                    number.getText().toString(), type,
	                    notes.getText().toString());
	      model.requery();
	    }
	  };
	  
	  private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent,
	                              View view, int position,
	                              long id) {
	      model.moveToPosition(position);
	      name.setText(entity.getName(model));
	      number.setText(entity.getNumber(model));
	      notes.setText(entity.getNotes(model));
	      
	      if (entity.getType(model).equals("improtant")) {
	        types.check(R.id.important);
	      }
	      else if (entity.getType(model).equals("friend")) {
	        types.check(R.id.friend);
	      }
	      else {
	        types.check(R.id.know);
	      }
	      
	      getTabHost().setCurrentTab(1);
	    }
	  };
	  
	  class RestaurantAdapter extends CursorAdapter {
	    RestaurantAdapter(Cursor c) {
	      super(ContactActivity.this, c);
	    }
	    
	    @Override
	    public void bindView(View row, Context ctxt,
	                         Cursor c) {
	    	ContacttHolder holder=(ContacttHolder)row.getTag();
	      
	      holder.populateFrom(c, entity);
	    }
	    
	    @Override
	    public View newView(Context ctxt, Cursor c,
	                         ViewGroup parent) {
	      LayoutInflater inflater=getLayoutInflater();
	      View row=inflater.inflate(R.layout.row_contact, parent, false);
	      ContacttHolder holder=new ContacttHolder(row);
	      
	      row.setTag(holder);
	      
	      return(row);
	    }
	  }
	  
	  static class ContacttHolder {
	    private TextView name=null;
	    private TextView number=null;
	    private ImageView icon=null;
	    
	    ContacttHolder(View row) {
	      name=(TextView)row.findViewById(R.id.name);
	      number=(TextView)row.findViewById(R.id.phonenumber);
	      icon=(ImageView)row.findViewById(R.id.icon);
	    }
	    
	    void populateFrom(Cursor c, ContactEntity entity) {
	      name.setText(entity.getName(c));
	      number.setText(entity.getNumber(c));
	  
	      if (entity.getType(c).equals("improtant")) {
	        icon.setImageResource(R.drawable.ball_red);
	      }
	      else if (entity.getType(c).equals("take_out")) {
	        icon.setImageResource(R.drawable.ball_yellow);
	      }
	      else {
	        icon.setImageResource(R.drawable.ball_green);
	      }
	    }
	  }
	}
